# -*- coding: utf-8 -*-
from flask import Flask,render_template,request,send_file
import re,subprocess


# -------------------------定义变量容器-------------------------------------
app=Flask(__name__)

names = list()
items = dict()
pattern = r"[\u4e00-\u9fa5]{2,5}"
pattern_mac = r"[0-9a-fA-F]{2}-.+[0-9a-fA-F]{2}"


@app.route('/login',methods = ['POST'])
def login():

    # ----------------------获取ip和mac地址--------------------------------
    ip = request.remote_addr
    cmd = r"arp -a %s" % (ip)
    shell_output = subprocess.check_output(cmd, shell=True).decode('gbk')
    mac_address = re.search(pattern=pattern_mac, string=shell_output)
    if mac_address is not None and not items.__contains__(mac_address.group(0)):
        print('Visitor Media Access Control Address:' + mac_address.group(0))
        print('Visitor Internet Protocol Address' + ip)

        # ----------------------提交表单和打卡--------------------------------
        username = request.form.get('username')
        name = re.search(pattern=pattern, string=username)
        if name is not None:
            print(name.group(0) + '已签到！')
            items[mac_address.group(0)] =name
            names.append(name.group(0))
            return send_file('./static/success.jpg', mimetype='image/gif')

        else:
            return "签到失败！请输入中文姓名(2-5个字)"


    else:
        return "不得重复签到！"



@app.route('/',methods = ['POST','GET'])
def index():
    return render_template("clockin.html")



@app.route('/result',methods=['POST','GET'])
def result():
    return render_template('clockin_status.html',names = names)


if __name__=="__main__":
    app.run(host = "0.0.0.0",port=80,debug=True)







